//
//  MOMyShenPiWebViewViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/9/20.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyShenPiWebViewViewController.h"
#import <WebKit/WebKit.h>

@interface MOMyShenPiWebViewViewController ()<WKNavigationDelegate,WKScriptMessageHandler>

@property(nonatomic,strong)WKWebView *webView;

@end

@implementation MOMyShenPiWebViewViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"backClick"];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"backClick"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = KGrayColor_f8f8f8;
    
    self.title = self.titleStr;
    
    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [closeButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    [closeButton addTarget:self action:@selector(closeCurrentVc) forControlEvents:UIControlEventTouchUpInside];
    [closeButton setImage:IMAGE(@"companyManage_close") forState:UIControlStateNormal];
    [closeButton setImage:IMAGE(@"companyManage_close") forState:UIControlStateHighlighted];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
    
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    
    [self creatView];
}

-(void)closeCurrentVc
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatView
{
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight)];
    self.webView.opaque = NO;
    [self.webView sizeToFit];
    self.webView.navigationDelegate = self;
    self.webView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.webView];
    
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self.urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    [self.webView loadRequest:request];
    
    
    
}

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
    }
}


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    if ([message.name isEqualToString:@"backClick"]) {
        //  message.body js传过来的参数 为id类型 NSArray,NSDictionary,NSString等等
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
